package com.hh.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hh.commonutils.Result;
import com.hh.eduservice.entity.EduChapter;
import com.hh.eduservice.entity.EduVideo;
import com.hh.eduservice.entity.chapter.ChapterVo;
import com.hh.eduservice.entity.chapter.VideoVo;
import com.hh.eduservice.mapper.EduChapterMapper;
import com.hh.eduservice.mapper.EduVideoMapper;
import com.hh.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.eduservice.service.client.VideoIntervice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huhuan
 * @since 2021-10-20
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduChapterMapper eduChapterMapper;
    @Autowired
    private EduVideoMapper eduVideoMapper;
    @Autowired
    private VideoIntervice videoIntervice;

    @Override
    public List<ChapterVo> getChapterById(String courseId) {
        QueryWrapper chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", courseId);
        ArrayList<EduChapter> chapterList = (ArrayList) eduChapterMapper.selectList(chapterWrapper);

        QueryWrapper videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        ArrayList<EduVideo> videoList = (ArrayList) eduVideoMapper.selectList(videoWrapper);

        List<ChapterVo> result = new ArrayList<>();
        for (EduChapter eduChapter : chapterList) {
            ArrayList<VideoVo> videoVos = new ArrayList<>();
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);

            for (EduVideo eduVideo : videoList) {
                if (chapterVo.getId().equals(eduVideo.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVos.add(videoVo);
                }
                chapterVo.setChildren(videoVos);
            }
            result.add(chapterVo);
        }
        return result;
    }

    @Override
    public void addChapter(EduChapter eduChapter) {
        eduChapterMapper.insert(eduChapter);
    }

    @Override
    public EduChapter getEduChapterById(String chapterId) {
        return eduChapterMapper.selectById(chapterId);
    }

    @Override
    public Result removeChapterById(String chapterId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        ArrayList<EduVideo> list = (ArrayList<EduVideo>) eduVideoMapper.selectList(wrapper);
        ArrayList<String> videoSourceIds = new ArrayList<>();
        try {
            if (list.isEmpty()) {
                eduChapterMapper.deleteById(chapterId);
            } else {
                for (EduVideo eduVideo : list) {
                    if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())) {
                        videoSourceIds.add(eduVideo.getVideoSourceId());
                    }
                }
                // 删除阿里云的视频
                String join = StringUtils.join(videoSourceIds, ",");
                Result result = videoIntervice.deleteUploadVideo(join);
                if (result.getCode() != 20000) {
                    throw new RuntimeException("EduChapter删除视频出错，熔断器的错误是:"+result.getData().get("msg"));
                }
                // 删除小节信息
                QueryWrapper wrapper1 = new QueryWrapper();
                wrapper1.eq("chapter_id", chapterId);
                eduVideoMapper.delete(wrapper1);
                // 删除章节信息
                eduChapterMapper.deleteById(chapterId);
            }
            return Result.success().data("msg", "删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
